package com.jianghu;

import java.util.ArrayList;
import java.util.HashMap;
import com.jianghu.helpers.GetLineFromtxt;
import com.jianghu.view.HeaderOfUserTrends;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class UserTrendsManual extends Activity {

	private String user;
	private String type;
	private ListView listView;
	private boolean surplus = true;
	private int count = 0;
	private Thread mThread;
	private ProgressBar progressBar;
	private TextView txtMore;
	private TextView txtUser;
	private ImageView usericon;
	View loadingLayout;
	ArrayList<HashMap<String, Object>> listItem;
	SimpleAdapter listItemAdapter;
	private String userTrends;
	private String UserName;
	private Button attention;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.usertrends);
		Intent intent = getIntent();
		type = intent.getStringExtra("type").toString();
		user = intent.getStringExtra("item").toString();
		userTrends = user + "的" + this.getString(R.string.trends);
		TextView whoestrend = (TextView) findViewById(R.id.whosetrends);
		whoestrend.setText(userTrends);
		whoestrend.setTextSize(18);

		ImageButton home = (ImageButton) findViewById(R.id.homebuttonofusertrends);
		usericon = (ImageView) findViewById(R.id.usericon);
		attention = (Button) findViewById(R.id.attention);
		if (type.equals("trends")) {
			home.setBackgroundResource(R.drawable.back);
		}
		if (user.equals("我")) {
			UserName = "设计工坊";

		} else {
			UserName = user;
			usericon.setImageDrawable(this.getResources().getDrawable(
					R.drawable.icon_1));
			attention.setText("关注");
			attention.setBackgroundResource(R.drawable.attention);
			attention.setOnClickListener(new attentionClickListener());

		}
		txtUser = (TextView) findViewById(R.id.username);
		txtUser.setText(UserName);
		home.setOnClickListener(new homeClickListener());

		ImageButton refresh = (ImageButton) findViewById(R.id.refreshbuttonofusertrends);
		refresh.setOnClickListener(new refreshClickListener());

		listView = (ListView) findViewById(R.id.listViewOfUserTrends);
		listItem = new ArrayList<HashMap<String, Object>>();

		for (int i = 0; i < 20; i++) {
			count++;
			HashMap<String, Object> map = new HashMap<String, Object>();
			String str = GetLineFromtxt.readAppointedLineNumber(this, i + 1);
			if (str == null) {
				surplus = false;
				break;
			}

			map.put("UserName", UserName);
			map.put("timeoftrends", i + "小时前");
			map.put("contentsoftrends", str);
			listItem.add(map);
		}

		listItemAdapter = new SimpleAdapter(this, listItem,
				R.layout.listview_user_trends, new String[] { "UserName",
						"timeoftrends", "contentsoftrends" }, new int[] {
						R.id.UserNameofTrends, R.id.timeoftrends,
						R.id.contentsoftrends });
		HeaderOfUserTrends headerView = new HeaderOfUserTrends(this);
		headerView.setTextViewforFans("450");
		headerView.setTextViewforLikes("50");
		headerView.setTextViewforShares("10");
		headerView.setTextViewforFriends("150");

		listView.addHeaderView(headerView, null, false);
		loadingLayout = ((LayoutInflater) this
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(
				R.layout.footer_listview_user_trends, null, false);
		listView.addFooterView(loadingLayout, null, true);
		listView.setAdapter(listItemAdapter);

		loadingLayout.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				DownloadMore downloadMore = new DownloadMore();
				downloadMore.execute((Void) null);
				// progressBar = (ProgressBar)
				// loadingLayout.findViewById(R.id.progressBarofUserTrends);
				// progressBar.setVisibility(View.VISIBLE);
				// txtMore = (TextView)
				// loadingLayout.findViewById(R.id.moreofusertrends);
				// txtMore.setVisibility(View.GONE);
				// if (mThread == null || !mThread.isAlive()) {
				// mThread = new Thread() {
				// @Override
				// public void run() {
				// try {
				// loadRemnantListItem(count);
				// Thread.sleep(5000);
				// } catch (InterruptedException e) {
				// e.printStackTrace();
				// }
				// Message message = new Message();
				// message.what = 1;
				// handler.sendMessage(message);
				// }
				// };
				// mThread.run();
				// }

			}

			private Handler handler = new Handler() {
				@Override
				public void handleMessage(Message msg) {
					switch (msg.what) {
					case 1:
						listItemAdapter.notifyDataSetChanged();
						if (surplus == false) {

							listView.removeFooterView(loadingLayout);
						} else {
							progressBar = (ProgressBar) loadingLayout
									.findViewById(R.id.progressBarofUserTrends);
							progressBar.setVisibility(View.GONE);
							txtMore = (TextView) loadingLayout
									.findViewById(R.id.moreofusertrends);
							txtMore.setVisibility(View.VISIBLE);
						}
						break;
					default:
						break;
					}
				}
			};

		});

	}

	class DownloadMore extends AsyncTask<Void, Void, Void> {
		@Override
		protected void onPreExecute() {
			progressBar = (ProgressBar) loadingLayout
					.findViewById(R.id.progressBarofUserTrends);
			progressBar.setVisibility(View.VISIBLE);
			txtMore = (TextView) loadingLayout
					.findViewById(R.id.moreofusertrends);
			txtMore.setVisibility(View.GONE);
			super.onPreExecute();
		}

		@Override
		protected Void doInBackground(Void... arg0) {
			loadRemnantListItem(count);
			return null;
		}

		@Override
		protected void onPostExecute(Void result) {
			listItemAdapter.notifyDataSetChanged();
			if (surplus == false) {

				listView.removeFooterView(loadingLayout);
			} else {
				progressBar = (ProgressBar) loadingLayout
						.findViewById(R.id.progressBarofUserTrends);
				progressBar.setVisibility(View.GONE);
				txtMore = (TextView) loadingLayout
						.findViewById(R.id.moreofusertrends);
				txtMore.setVisibility(View.VISIBLE);
			}
			super.onPostExecute(result);
		}

		private void loadRemnantListItem(int totalItemCount) {

			int max = totalItemCount + 20;
			for (int i = totalItemCount; i < max; i++) {

				HashMap<String, Object> map = new HashMap<String, Object>();
				String str = GetLineFromtxt.readAppointedLineNumber(
						UserTrendsManual.this, i + 1);
				if (str == null) {
					surplus = false;
					break;
				}
				count++;
				map.put("UserName", UserName);
				map.put("timeoftrends", i + "小时前");
				map.put("contentsoftrends", str);
				listItem.add(map);
			}
		}
	}

	class homeClickListener implements OnClickListener {

		@Override
		public void onClick(View v) {
			Intent intent = new Intent();
			if (type.equals("trends")) {
				intent.setClass(UserTrendsManual.this, FriendsTrendsAuto.class);
			} else {
				intent.setClass(UserTrendsManual.this, MainPage.class);
			}
			UserTrendsManual.this.startActivity(intent);
		}
	}

	class refreshClickListener implements OnClickListener {
		private ProgressDialog progressDialog;

		@Override
		public void onClick(View v) {

			progressDialog = ProgressDialog.show(UserTrendsManual.this,
					"刷新中..", "请稍等...", true, false);

			new Thread() {

				@Override
				public void run() {
					try {
						Thread.sleep(5000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					handlerRefresh.sendEmptyMessage(0);
				}
			}.start();
		}

		private Handler handlerRefresh = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				listView.setSelection(0);
				progressDialog.dismiss();
			}
		};

	}

	class attentionClickListener implements OnClickListener {

		@Override
		public void onClick(View v) {
			String text = attention.getText().toString();
			if (text.equals("关注")) {
				attention.setText("取消关注");
				attention.setBackgroundResource(R.drawable.attention_cancel);
			} else {
				attention.setText("关注");
				attention.setBackgroundResource(R.drawable.attention);
			}

		}

	}

}